using System;
using Gtk;
using Pango;

public partial class MainWindow : Gtk.Window {
	
	public MainWindow () : base(Gtk.WindowType.Toplevel) {
		Build();
		hboxFind.Visible = false;
		
		FontDescription fontDescription = new FontDescription();
			fontDescription.Family = "Courier";
				txtSource.ModifyFont(fontDescription); 
	}

	protected void OnDeleteEvent (object sender, DeleteEventArgs a) {
		Application.Quit();
		a.RetVal = true;
	}
	protected virtual void mnuHelpAboutGnashDevelopAction_onClick (object sender, System.EventArgs e) {
		GnashDevelop.AboutDialog aboutDialog = new GnashDevelop.AboutDialog();
		aboutDialog.Show();
	}
	
	protected virtual void mnuSave_onClick (object sender, System.EventArgs e) {
		FileChooserDialog chooser = new FileChooserDialog(
				"Save File", 
				this,
				FileChooserAction.Save,
				"Cancel", ResponseType.Cancel,
				"Save", ResponseType.Accept );
		
		if (chooser.Run() == (int) ResponseType.Accept) {
			// Open the file for reading.
			System.IO.FileStream file = System.IO.File.Open(chooser.Filename, System.IO.FileMode.OpenOrCreate);
			
			// Copy the contents into the logTextView
			//file.Write(txtSource.Buffer.Text., 0, txtSource.Buffer.Text.Length);
			
			// Set the MainWindow Title to the filename.
			this.Title = "GnashDevelop - " + chooser.Filename.ToString();
			
			file.Close();
		}
		chooser.Destroy();
		mnuSave.Sensitive = false;
	}
	
	protected virtual void mnuOpen_onClick (object sender, System.EventArgs e) {
		FileChooserDialog chooser = new FileChooserDialog(
				"Open File", 
				this,
				FileChooserAction.Open,
				"Cancel", ResponseType.Cancel,
				"Open", ResponseType.Accept );
		
		if (chooser.Run() == (int) ResponseType.Accept) {
			System.IO.StreamReader file = System.IO.File.OpenText( chooser.Filename );
			
			// Copy the contents into the logTextView
			txtSource.Buffer.Text = file.ReadToEnd();
			
			this.Title = "GnashDevelop - " + chooser.Filename.ToString();
			
			file.Close();
		}
		chooser.Destroy();
	}
	
	protected virtual void txtSource_onKeyPress (object o, Gtk.KeyPressEventArgs args) {
		mnuSave.Sensitive = true;
	}
	
	protected virtual void btnFindClose_onClick (object sender, System.EventArgs e) {
		hboxFind.Visible = false;
	}
	
	protected virtual void mnuSearchQuickFind_onClick (object sender, System.EventArgs e) {
		hboxFind.Visible = true;
		txtSource.GrabFocus();
	}
	
	protected virtual void btnFindNext_onClick (object sender, System.EventArgs e) {
		//hboxFind.FocusChild = txtSource;
	}
}
